let schoolModel = require('school')
var gradeModel = require("grade.js")

const KLASSES = [{ "value": "1", "name": "(1) 班" },
{ "value": "2", "name": "(2) 班" },
{ "value": "3", "name": "(3) 班" },
{ "value": "4", "name": "(4) 班" },
{ "value": "5", "name": "(5) 班" },
{ "value": "6", "name": "(6) 班" },
{ "value": "7", "name": "(7) 班" },
{ "value": "8", "name": "(8) 班" },
{ "value": "9", "name": "(9) 班" },
{ "value": "10", "name": "(10) 班" },
{ "value": "11", "name": "(11) 班" },
{ "value": "12", "name": "(12) 班" }]

const CURRENT_ALL_CLASS_KEY = 'currentAllClass' // 保存用户有关联的所有班级
const CURRENT_CLASS_KEY = 'currentClass' // 保存用户当前选择的班级
const SHOW_NOTICE_KLASS_KEY = 'showNoticeKlass' // 当前查看哪个班级的通知

/**
 * 根据序号获取班级对象
 */
function getKlassByIndex(index) {
  return KLASSES[index]
}

function getKlassByKlassId(classId) {
  return KLASSES[parseInt(classId) - 1]
}

function getGrade(gradeIndex) {
  return gradeModel.getGradeByIndex(parseInt(gradeIndex) - 1)
}

function getCurrentAllKlass() {
  return wx.getStorageSync(CURRENT_ALL_CLASS_KEY)
}

function setCurrentAllKlass(klasses) {
  // let currentKlass = getCurrentKlass()
  // if (currentKlass) {
  //   for (let index = 0, length = klasses.length; index < length; index++) {
  //     if (currentKlass.sid == klasses[index].sid) {
  //       setCurrentKlass(klasses[index])
  //       break
  //     }
  //   }
  // }
  

  wx.setStorageSync(CURRENT_ALL_CLASS_KEY, klasses)
}

// function getCurrentKlass() {
//   return wx.getStorageSync(CURRENT_CLASS_KEY)
// }
//
// function setCurrentKlass(klass) {
//   wx.setStorageSync(CURRENT_CLASS_KEY, klass)
// }


function setShowNoticeKlass(klass) {
  wx.setStorageSync(SHOW_NOTICE_KLASS_KEY, klass)
}

function getShowNoticeKlass() {
  let klass = wx.getStorageSync(SHOW_NOTICE_KLASS_KEY)  
  if (null == klass.sid) {
    klass = getDefaultAllKlassItem()
  }

  return klass
}

/**
 *  重置当前查看某个班的通知
 *  删除班级成功后，需要对当前查看班级进行更新，如果当前查看班级是删除的班级，则更新为查看所有班级
 */
function resetShowNoticeKlass(deletedKlass) {
  let currentKlass = getShowNoticeKlass()

  if (currentKlass.sid == deletedKlass.sid) {
    setShowNoticeKlass(getDefaultAllKlassItem())
  }
}

function getKlassUIData(klass = null) {
  // if (null == klass) {
  //     klass = getCurrentKlass();
  // }
  
  if (klass) {
    if (schoolModel.CATEGORY_PRIMARY_SCHOOL == klass.category) {
      klass.uiname = getGrade(klass.grade).CN + '' + getKlassByKlassId(klass.index).name;
    } else if (schoolModel.CATEGORY_PRIMARY_SCHOOL == klass.category) {
      klass.uiname = klass.name
    }
      
  }

  return klass;
}

function getAllKlassUIData() {
  var klasses = getCurrentAllKlass();
  if (klasses) {
    var length = klasses.length;
    for (var index = 0; index < length; index++) {
        if (schoolModel.CATEGORY_PRIMARY_SCHOOL == klasses[index].category) {
          klasses[index].uiname = getGrade(klasses[index].grade).CN + '' + getKlassByKlassId(klasses[index].index).name;
        }else {
          klasses[index].uiname = klasses[index].name
        }
    }
  }

  return klasses;
}

function getKlassByKlassSid(klassSid) {
  let klasses = getAllKlassUIData(),klass = null
  for (let index = 0, length = klasses.length; index < length; index++) {
    if (klassSid == klasses[index].sid) {
      klass = klasses[index]
      break
    }
  }

  return klass
}

function getDefaultAllKlassItem() {
  let klass = {
    'sid': '0', 'schoolName': '', 'uiname': '所有班级', 'klassName':'所有班级'}
  return klass
}

module.exports = {
  CLASSES: KLASSES,
  getClassByIndex: getKlassByIndex,
  setCurrentAllClass: setCurrentAllKlass,
  getCurrentAllClass: getCurrentAllKlass,
  // setCurrentClass: setCurrentKlass,
  // getCurrentClass: getCurrentKlass,
  getGrade: getGrade,
  getClassByClassId: getKlassByKlassId,
  getKlassUIData: getKlassUIData,
  getAllKlassUIData: getAllKlassUIData,
  getKlassByKlassSid: getKlassByKlassSid,
  getShowNoticeKlass: getShowNoticeKlass,
  setShowNoticeKlass: setShowNoticeKlass,
  getDefaultAllKlassItem: getDefaultAllKlassItem,
  resetShowNoticeKlass: resetShowNoticeKlass
}